package com.waner.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waner.reggie.common.CustomException;
import com.waner.reggie.common.R;
import com.waner.reggie.dto.DishDto;
import com.waner.reggie.entity.Category;
import com.waner.reggie.entity.Dish;
import com.waner.reggie.entity.DishFlavor;
import com.waner.reggie.service.CategoryService;
import com.waner.reggie.service.DishFlavorService;
import com.waner.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        // 调用Dto的分页查询
        Page<DishDto> pageInfo = dishService.pageWithCategoryName(page,pageSize,name);

        return R.success(pageInfo);
    }

    /**
     * 根据id查询菜品以及口味
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> quryById(@PathVariable Long id){
        log.info("根据id查询菜品...");

        DishDto dishDto = dishService.getDishByIdWithFlavor(id);

        return R.success(dishDto);
    }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavors(dishDto);
        return R.success("新增菜品成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavors(dishDto);
        return R.success("修改信息成功！");
    }

    /**
     * 删除或者批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        dishService.deleteWithFlavors(ids);

        return R.success("删除成功！");
    }

    /**
     * 将菜品的状态修改
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> stateTo0(@PathVariable int status, @RequestParam List<Long> ids){

        // 进行属性的修改
        log.info("修改菜品的状态....{}",ids);

        for(Long id : ids){
            Dish dish = dishService.getById(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }

        return R.success("操作成功！");
    }


    /**
     * 根据categoryId查询
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){

        // 接口
        log.info("根据菜品种类id查询菜品以及菜品的口味...");

        List<DishDto> dishDtos = dishService.listByCategoryIdWithFlavors(categoryId);

        return R.success(dishDtos);
    }
}
